package com.desiy.controller;

import com.desiy.pojo.dto.ResponseDTO;
import com.desiy.service.AdminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @author Desiy
 * @date 2020/5/19 15:41
 */
@Controller
public class MyController {

    @RequestMapping({"/","/index"})
    String index(){
        return "index";
    }

    @RequestMapping("/user/login")
    String login(@RequestParam("username") String username,
                 @RequestParam("password") String password,
                 Model model, HttpSession session){
        //获取当前对象
        Subject user = SecurityUtils.getSubject();
        //封装用户的登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            user.login(token);
            return "redirect:/main.html";
        }catch (UnknownAccountException e){
            model.addAttribute("errorMsg","用户名不存在或密码错误。");
            return "index";
        }catch (IncorrectCredentialsException e){
            model.addAttribute("errorMsg","用户名不存在或密码错误。");
            return "index";
        }
    }
}
